<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/2
 * Time: 14:15
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Member extends Base
{
    /**
     * 用户首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $user_phone = trim(input('user_phone'));
            $user_username = trim(input('user_username'));
            $user_status = input('user_status');
            
            $where['user_status'] = ['between', [1,2]];
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_status) && $user_status != ''){
                $where['user_status'] = $user_status;
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->where($where)->count();// 获取总条数
            $lists = Db::name('u_user')
                ->field('user_id, user_phone, user_username, user_image, user_status, user_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('user_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 编辑用户
     * @return mixed|\think\response\Json
     */
    public function editUser(){
        $user_id = input('user_id');
        if(Request::instance()->isPost()){
            $user_username = input('user_username');
            $user_phone = trim(input('user_phone'));
            if(!preg_match("/^1[3456789]\d{9}$/", $user_phone)) {
                return json(['code' => 0, 'msg' => '请输入正确格式的手机号']);
            }
            
            $have = Db::table('up_u_user')->where('user_phone', $user_phone)->find();
            if($have) return json(['code' => 100, 'msg' => '该手机号已绑定其它账号']);
            
            $update = [
                'user_id' => $user_id,
                'user_username' => $user_username,
                'user_phone' => $user_phone,
                'user_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_u_user')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $user = Db::table('up_u_user')->where('user_id', $user_id)->field('user_username, user_phone')->find();
        $this->assign([
            'user' => $user,
            'user_id' => $user_id
        ]);
        return $this->fetch();
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function userDetail(){
        $user_id = input('user_id');
        $user = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_phone, user_username, user_image, user_time, user_status, user_wx_nick, 
            user_wx_image')
            ->find();
        return $this->fetch('member/userdetail', ['user' => $user]);
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function userFreeze(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function userUsing(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
            
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function userDel(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}